package org.example.mail.send;

import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;

public class SendMailWithHTML {

    public static void main(String[] args) throws Exception {
        final String smtp = "imap.qq.com";
        final String username = "974332738@qq.com";
        final String password = "pdvefuaksjycbegh";
        final String from = "974332738@qq.com";
        final String to = "q333tita@163.com";
        SendMail sender = new SendMail(smtp, username, password);
        Session session = sender.createTLSSession();
        Message message = createHtmlMessage(session, from, to, "Java HTML邮件", "<h1>Hello</h1><p>这是一封<u>javamail</u>HTML邮件！</p>");
        Transport.send(message);
    }

    static Message createHtmlMessage(Session session, String from, String to, String subject, String body) throws MessagingException {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject(subject, "UTF-8");
        message.setText(body, "UTF-8", "html");
        return message;
    }

}
